using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Anarchy
{

    class FileExistsException : Exception
    {
        public FileExistsException() { }
        public FileExistsException(string message) : base(message) { }
    }

    class FSWrapper
    {

        string ArchivePath, ThumbsPath;

        // Constructor
        public FSWrapper(string _ArchivePath, string _ThumbsPath)
        {
            ArchivePath = _ArchivePath;
            ThumbsPath = _ThumbsPath;
        }

        // Could we add a 'replace' param to this to stop the FileExistsException being thrown? Maybe.
        public void Add(string AbsolutePath, string Path)
        {
            Add(AbsolutePath, Path, false);
        }
        public void Add(string AbsolutePath, string Path, bool Move)
        {

            // Does old exist?
            if (File.Exists(AbsolutePath))
                // Does new path exist?
                if (!File.Exists(GetPath(Path)))
                {

                    // Move or copy?
                    if (Move)
                        File.Move(AbsolutePath, GetPath(Path));
                    else
                        File.Copy(AbsolutePath, GetPath(Path));

                    // We'll need to add code to generate a thumbnail here.

                }
                else
                    throw new FileExistsException("File '" + Path + "' already exists when trying to add '" + AbsolutePath + "' to this location in archive.");
            else
                throw new FileNotFoundException("Cannot find file, '" + AbsolutePath + "', when attemtping to add to archive.");


        }

        public void Delete(string Path)
        {

            // Exist?
            if (File.Exists(GetPath(Path)))
                File.Delete(GetPath(Path));
            else
                throw new FileNotFoundException("Cannot find file, '" + Path + "' to delete from archive.");

            // Todo: Delete thumbz.

        }

        public void Move(string OldPath, string NewPath)
        {
            Copy(OldPath, NewPath, true);
        }
        public void Move(string OldPath, string NewPath, bool Replace)
        {
            Copy(OldPath, NewPath, true, Replace);
        }

        public void Copy(string OldPath, string NewPath)
        {
            Copy(OldPath, NewPath, false);
        }
        public void Copy(string OldPath, string NewPath, bool Replace)
        {
            Copy(OldPath, NewPath, false, Replace);
        }
        private void Copy(string OldPath, string NewPath, bool Move, bool Replace)
        {
            
            // OldPath exist?
            if (File.Exists(GetPath(OldPath)))
            {

                // NewPath exist?
                if ((!File.Exists(NewPath)) || (!Replace))
                {

                    // Move or copy?
                    if (Move)
                        File.Move(GetPath(OldPath), GetPath(NewPath));
                    else
                        File.Copy(GetPath(OldPath), GetPath(NewPath));

                    // Todo: Update thumbz.

                }
                else
                    throw new FileExistsException("File '" + NewPath + "' already exists in archive whent trying to " + (Move ? "move" : "copy") + " file from '" + OldPath + "'.");

            }
            else
                throw new FileNotFoundException("Cannot find '" + OldPath + "' when trying to move it to '" + NewPath + "'.");

        }

        public Stream GetStream(string Path)
        {
            return GetStream(new string[]{Path})[0];
        }
        public Stream[] GetStream(string[] Path)
        {

            // Var!
            Stream[] s = new Stream[Path.Length];

            // Get each stream.
            for (int i = 0; i < Path.Length; i++)
                // Exist?
                if (File.Exists(GetPath(Path[i])))
                    // Get stream
                    s[i] = File.Open(GetPath(Path[i]), FileMode.Open);
                else
                    throw new FileNotFoundException("Cannot find file '" + Path[i] + "' in archive when trying to get it's stream.");

            return s;
            
        }

        private string GetPath(string Path)
        {
            return ArchivePath + Path;
        }

    }
}
